package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐接口")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @ApiOperation("套餐起售、停售")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result status(@PathVariable Integer status,Long id){
        setmealService.updateStatus(status,id);
        return Result.success();
    }

    @PutMapping()
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result updateSermeal(@RequestBody SetmealDTO setmealDTO){
        setmealService.updateSermeal(setmealDTO);
        return Result.success();
    }

    @DeleteMapping()
    @ApiOperation("删除套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deleteSermeal(@RequestParam List<Long> ids){
        log.info("ids:{}",ids);
        setmealService.deleteSermeal(ids);
        return Result.success();
    }
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result selectSermeal(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("setmealPageQueryDTO:{}",setmealPageQueryDTO);
        PageResult pageResult =  setmealService.selectSermeal(setmealPageQueryDTO);
        return Result.success(pageResult);
    }
    @GetMapping("/{id}")
    @ApiOperation("查询套餐")
    public Result selectById(@PathVariable Long id){
        log.info("id:{}",id);
        SetmealVO setmealVO =  setmealService.selectById(id);
        return Result.success(setmealVO);
    }



    @ApiOperation("新增套餐")
    @PostMapping()
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO){
        log.info("setmealDTO:{}",setmealDTO);
       setmealService.addSetmeal(setmealDTO);
        return Result.success();
    }
}
